﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Configuration;

namespace FileOperationWinform
{
    public partial class FrmExcept : Form
    {
        public FrmExcept()
        {
            InitializeComponent();
        }

        private static readonly string skin = ConfigurationManager.AppSettings["SKin"];
        private static readonly string exceptlocation = ConfigurationManager.AppSettings["Except"];
        private void FrmExcept_Load(object sender, EventArgs e)
        {
            FileOpera ft = new FileOpera();
            string st=ft.SkinLoad();
            if (!(string.Equals(st, "Error")))
            {
                skinEngine1.SkinFile = System.IO.Path.Combine(Application.StartupPath, st);
            }
            string tempstr = Path.Combine(Application.StartupPath, exceptlocation);
            if (File.Exists(tempstr))
            {
                string[] str = new string[2];
                string[] temp = new string[3];
                //string[] temp1=new string[]
                FileStream fs = new FileStream(tempstr, FileMode.Open, FileAccess.Read);
                StreamReader sw = new StreamReader(fs);
                string line = sw.ReadLine();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; line != null; i++)
                {
                    temp = line.Split(',');
                    str[0] = temp[1];
                    str[1] = temp[2];
                    ListViewItem item = new ListViewItem(str, 0);
                    listView1.Items.Add(item);
                    //str1.Split(",");
                    line = sw.ReadLine();
                }
                sw.Close();
                fs.Close();
            }


        }
        private void btnExceptAdd_Click(object sender, EventArgs e)
        {
            FrmExceDetail frm = new FrmExceDetail();
            frm.GetForm(this);
            frm.ShowDialog();
            this.listView1.Focus();
            //frm.Focus();
        }

        private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            ColumnHeader header = this.listView1.Columns[e.ColumnIndex];
            if (header.Width==0)
            {
                e.Cancel = true;
            }
            if (header.Index==0)
            {
                e.Cancel = true;
            }
            /*
            else if(header.Name=="biaoti1")
            */
            e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
        }

        private void btnExceptDel_Click(object sender, EventArgs e)
        {
            int index=0;
            if (this.listView1.SelectedItems.Count>0)
            {
                index = this.listView1.SelectedItems[0].Index;
                listView1.Items[index].Remove();

                FileOpera fs = new FileOpera();
                fs.FileDel(index);

            }

        }

        private void btnExceptEdit_Click(object sender, EventArgs e)
        {
            int index = 0;
            if (this.listView1.SelectedItems.Count > 0)
            {
                index = this.listView1.SelectedItems[0].Index;
                string str = listView1.Items[index].SubItems[0].Text.ToString();
                string str1 = listView1.Items[index].SubItems[1].Text.ToString();

                FrmExceDetail frm = new FrmExceDetail(str,str1,index);
                frm.GetForm(this);
                frm.ShowDialog();
            }
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            //if (listView1.SelectedItems.Count==0)
            //{
            //    return;
            //}
            //listView1.BeginUpdate();
            //if (listView1.SelectedItems[0].Index>0)
            //{
            //    foreach (ListViewItem lvi in listView1.SelectedItems)
            //    {
            //        ListViewItem lviSeltedItem = lvi;
            //        int indexselext = lvi.Index;
            //        listView1.Items.RemoveAt(indexselext);
            //        listView1.Items.Insert(indexselext - 1,lviSeltedItem);

            //    }
            //}
            //listView1.EndUpdate();

            //if (listView1.Items.Count>0&&listView1.SelectedItems.Count>0)
            //{
            //    listView1.Focus();
            //    listView1.SelectedItems[0].Focused = true;
            //    listView1.SelectedItems[0].EnsureVisible();
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
